package com.sqsf.util;

import com.sqsf.javabean.User;
import mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class UserMapperTest {
    SqlSession session;
    UserMapper mapper;

    @Before
    public void before() {
        //1、创建SqlSessionFactory对象，也是单例模式的
        SqlSessionFactory factory = MyBatisUtil.getSqlSessionFactory();
        //2、创建SqlSession对象 true 自动提交事务 不写也没事 因为默认在sqlsession关闭时提交事务
        session = factory.openSession(true);
        //3、调用session的方法namespace的名字.id
        mapper = session.getMapper(UserMapper.class);

    }

    @After
    public void after() {
        //5、关闭资源
        session.close();
    }

    @Test
    public void selectAll(){
        for (User user : mapper.selectAll()) {
            System.out.println(user);
        }
    }
    @Test
    public void selectByUser(){
        User u=new User();
       // u.setUid(1);
        u.setUsername("a");
        u.setPassword("a");
        for (User user : mapper.selectByUser(u)) {
            System.out.println(user);
        }
    }

    @Test
    public void selectByUserOne(){
        User u=new User();
         u.setUid(1);
        u.setUsername("a");
        u.setPassword("a");
        for (User user : mapper.selectByUserOne(u)) {
            System.out.println(user);
        }
    }

    @Test
    public void updateByUser(){
        User u=new User();
//        u.setUid(3);
//        u.setUsername("a");
//        u.setPassword("a");
//        u.setPhone("a");
        mapper.updateByUser(u);
    }

    @Test
    public void trimTest(){

        mapper.trimTest();
    }

    @Test
    public void deleteByArray(){
        int[] array={4,5,6,7,8};
        mapper.deleteByArray(array);
    }

    @Test
    public void insertList(){
        List<User> list=new ArrayList<>();
        list.add(new User("a1","a1","a1"));
        list.add(new User("a2","a2","a2"));
        list.add(new User("a3","a3","a3"));
        list.add(new User("a4","a4","a4"));
        list.add(new User("a5","a5","a5"));
        mapper.insertList(list);
    }
}